<?php
namespace app\admin\controller;
use think\Controller;
use think\Session;
use app\common\RestController;
use app\common\tool\PasswordTools;
use app\constant\api;
use app\admin\model\Backs;
class UserBack extends RestController{
	 //开始注册
	 public function sing(){
	 	 $code=$data=$message=api\SUCCESS;
	 	 $input=request()->param();
	 	 // return json($input);
         $salt=PasswordTools::buildMask();
         $password=PasswordTools::encrypt($input['UserPasswd'],$salt);
         $result=$this->validate($input,'UserVerify.regist');
         if($result===true){
            $res=Backs::addInfo([
              'username'=>$input['user_name'],
              'password'=>$password,
              'password_salt'=>$salt,
              'email'=>$input['UserEmail'],
              'ctime'=>date('Y-m-d H:i:s',time()),
         	]);
         	if(!$res){
         		$code=api\DATA_INSERT_ERROR;
         	}
         }else{
         	$code=api\FAIL;
         	$message=$result;
         }
         return $this->setDirreturnVars($code,$message,$data);
	 }
	 //开始登录
	 public function login(){
         $code=$data=$message=api\SUCCESS;
	 	 $input=request()->param();
         $result=$this->validate($input,'UserVerify.login');
         if($result===true){
               if(captcha_check($input['verify'])){
                    $user=Backs::getInformation(['username'=>$input['user_name']]);
                    if($user){
                          if($user['password']==PasswordTools::encrypt($input['UserPassward'],$user['password_salt'])){
                          	unset($user['password']);
                          	unset($user['password_salt']);
                             Session::set('back_user',$user);
                          }else{
                          	$code=api\PASS_INCORRECT;
                          }
                    }else{
                    	$code=api\USER_NOT_EXIST;
                    }
               }else{
               	  $code=api\CAPTCHA_INCORRECT;
               } 
         }else{
         	$code=api\FAIL;
         	$message=$result;
         }
         return $this->setDirreturnVars($code,$message,$data);
	 }
	 //退出登录
	 public function out(){

	 }
}